<?php
// Title
$output  = "";
$output .= "<form action='#' method='post'>";
$movies  = getAllMovies();

if(!empty($_GET['movieid'])){
	$_POST['show_details'] = $_GET['movieid'];
}

// Actionhandler
if(isset($_GET['rating']) || isset($_POST['add_comment']) ||isset($_POST['edit'])  || isset($_POST['delete_comment']) || isset($_POST['delete_movie'])){
	if(!isset($_POST['edit'])){
		if(isset($_POST['add_comment_text']) && !empty($_POST['add_comment_text'])){
			addComment($_POST['add_comment_text']);
			$_POST['show_details'] = $_SESSION['current_movie'];
		} else if(isset($_GET['movie'])){
			setRating($_GET['movie'],$_GET['rating'],$_SESSION['user_id']);
			$_POST['show_details'] = $_SESSION['current_movie'];
		} else if(isset($_POST['delete_comment'])){
			deleteComment($_POST['delete_comment']);
			$_POST['show_details'] = $_SESSION['current_movie'];
		} else if(isset($_POST['delete_movie'])){
			deleteMovie();
		}
	}
}

if(isset($_POST['show_details'])){
	// Get all information
	$_SESSION['current_movie'] = $_POST['show_details'];
	$movie_name   = getMovieInformation($_POST['show_details'], "title");
	$description  = getMovieInformation($_POST['show_details'], "description");
	$language     = getMovieInformation($_POST['show_details'], "language");
	$regisseur    = getMovieInformation($_POST['show_details'], "regisseur_name");
	$rating       = getMovieInformation($_POST['show_details'], "rating_avarage");
	$rating_count = getMovieInformation($_POST['show_details'], "rating_count");
	$comments     = getComments($_POST['show_details']);
	$actors = getActorsFromMovie($_POST['show_details']);
	$offers = getOffersFromMovie($_POST['show_details']);

	// Show Information
	$output .= "<br><img src='img/movie_icon.png'>";
	if(isset($_GET['rating'])){
		$output .= "<font color=green><br>You're rating has been added (" . $_GET['rating'] . ")</font>";
	}
	$output .= "<br>Rate:  ";
	for($count=1;$count<11;$count++){
		$output .= "<a style='text-decoration:none' title=$count href='index.php?site=movie&movie=".$_POST['show_details']."&rating=$count'><img border='0' src='img/icon/rating.gif'> </a>";
	}
	$output .= "<br><a style='text-decoration:none;font-size:8pt;color:gray;' href='index.php?site=offer&movie=".$_POST['show_details']."'>Click here to sell this movie!</a>";
	$output .= "<br><br><h1>$movie_name</h1><a style='color:gray;font-size:10pt;'>Bewertung: <b>".number_format($rating,1)."</b> ($rating_count ratings)</a><br/><br/>";
	$output .= "<font size=2><b>Language: </b>" . $language."";
	$output .= "<br>";
	$output .= "<b>Regisseur: </b>" . $regisseur."<br><br>";
	$output .= "<b>Description: </b>";
	$output .= "<br><br>";
	$output .= "" . $description . "<br><br>";
	$output .= "<b>Actors: </b>";
	$output .= "<br><ul>";
	while($actor = mysql_fetch_array($actors)){
	$output .= "<li>" . $actor['name'] . "</li>";
	}
	$output .= "</ul>";
	
	$output .= "<br><b>Offers: </b>";
	$output .= "<br><ul>";
	while($offer = mysql_fetch_array($offers)){
	$output .= "<li><a style='color:black;text-decoration:none;font-size:9pt;' href='index.php?site=buy&offer=".$offer['offer_id']."'>Buy this movie for just <b>" . number_format($offer['price'],2) . " CHF </b></a></li>";
	}
	$output .= "</ul>";
	// Comments
	$output .= "<h4>Comments</h4>";
	if(isset($_POST['delete_comment'])){
		$output .= "<font color=green>Deleted</font>";
	}
	$output .= "<table width=50%>";
	$output .= "<tr>";
	$output .= "<th>Username</th>";
	$output .= "<th>Comment</th>";
	$output .= "</tr>";
	$output .= "<tr>";
	while($comment = mysql_fetch_array($comments)){
		if(!empty($comment['username'])){
			$can_del = getUserId_($comment['username']) == $_SESSION['user_id'];
			
			// If comment belongs to the user
			if($can_del){
				$comment_id = $comment['comment_id'];
				$output .= "<tr><td><b>".$comment['username']."</b></td><td> " . $comment['comment']."</td>";
				$output .= "<td><input type='image' title='Delete comment' src='img/icon/delete.gif' name='delete_comment' value=$comment_id /></td></tr>";
			}else{
				$output .= "<tr><td><b>".$comment['username']."</b></td><td> " . $comment['comment']."</td></tr>";
			}
		}
	}
	$output .= "</tr>";
	$output .= "</table>";
	$output .= "<br>";
	if(isset($_POST['add_comment'])){
		if(isset($_POST['add_comment_text']) && !empty($_POST['add_comment_text'])){
			$output .= "<font color=green>Comment has been added</font>" ;
		}else{
			$output .= "<font color=red>Insert a text</font>";
		}
	}
	$output .= "<table width=50%>";
	$output .= "<tr>";
	$output .= "<td colspan='3'>User '<b> " . getUserInformation($_SESSION['user_id'] , "username"). "</b>'</td>" ;
	$output .= "</tr>";
	$output .= "<tr>";
	$output .= "<td colspan='3'>";
	$output .= "<textarea name=add_comment_text style='width:100%'></textarea>";
	$output .= "<input type=submit name=add_comment value='Add Comment'>";
	$output .= "</td>";
	$output .= "</tr>";
	$output .= "</table>";
	$output .= "</font><br>";
	$output .= "<font site=8>" . add_back_link("movie") . "</font>";

}else if(isset($_POST['add'])){
	$regisseurs   = getRegisseurs('*');
	// Title
	$output .= "<h1>Add</h1>";

	// Show Information
	$output .= "<table width=80%>";
	$output .= "<tr>";
	$output .= "<th>Title</th>";
	$output .= "<th>Description</th>";
	$output .= "<th>Language</th>";
	$output .= "<th>Regisseur</th>";
	$output .= "<th></th>";
	$output .= "</tr>";
	$output .= "<tr>";
	$output .= "<td><input type=text name=title  /></td>";
	$output .= "<td><input type=text name=description  /></td>";
	$output .= "<td><input type=text name=language  /></td>";
	$output .= '<td><select name="regisseur">';
	while($regisseur = mysql_fetch_array($regisseurs)){
			$output .= '<option value="'.$regisseur['regisseur_id'].'" >'.$regisseur['name'].'</option>';
	}
	$output .= '</select></td>';
	$output .= "<td><input type=submit name='add_movie' value='Add movie' /></td>";
	$output .= "</tr>";
	$output .= "</table";
}else if(isset($_POST['edit'])){
	// Get all information
	$title        = getMovieInformation($_POST['edit'], "title");
	$description  = getMovieInformation($_POST['edit'], "description");
	$language     = getMovieInformation($_POST['edit'], "language");
	$regisseur_id = getRegisseurIdByName(getMovieInformation($_POST['edit'], "regisseur_name"));
	$regisseurs   = getRegisseurs('*');
	$actors   = getActors('*');

	// Title
	$output .= "<h1>Edit $title</h1>";

	// Show Information
	$output .= "<table width=80%>";
	$output .= "<tr>";
	$output .= "<th>MovieID</th>";
	$output .= "<th>Title</th>";
	$output .= "<th>Description</th>";
	$output .= "<th>Language</th>";
	$output .= "<th>Regisseur</th>";
	$output .= "<th></th>";
	$output .= "<th></th>";
	$output .= "</tr>";
	$output .= "<tr>";
	$output .= "<td>".$_POST['edit']."</td>";
	$output .= "<td><input type=text name=title value='$title' /></td>";
	$output .= "<td><input type=text name=description value='$description' /></td>";
	$output .= "<td><input type=text name=language value='$language' /></td>";
	$output .= '<td><select name="regisseur">';
	while($regisseur = mysql_fetch_array($regisseurs)){
		if($regisseur_id == $regisseur['regisseur_id']){
			$output .= '<option value="'.$regisseur['regisseur_id'].'"  SELECTED>'.$regisseur['name'].'</option>';
		}else{
			$output .= '<option value="'.$regisseur['regisseur_id'].'">'.$regisseur['name'].'</option>';
		}
	}
	$output .= '</select></td>';
	$output .= "<td><input type=submit name='update' value='Save Changes' /></td>";
	$output .= "<td><input type=submit name='delete_movie' value='Delete movie' /></td>";
	$output .= "</tr>";
	$output .= "</table";
	$output .= "<input type=hidden name=id value='".$_POST['edit']."' />" ;
	$_SESSION['movie_to_del'] = $_POST['edit'];
	
	// Title
	$output .= "<h1>Add an actor to $title</h1><br>";
	$output .= "<table width=80%>";
	$output .= "<tr>";
	$output .= "<th>Actor Name</th>";
	$output .= "<th></th>";
	$output .= "</tr>";
	$output .= "<tr>";
	$output .= '<td><select name="actor">';
	while($actor = mysql_fetch_array($actors)){
			$output .= '<option value="'.$actor['actor_id'].'">'.$actor['name'].'</option>';
	}
	$output .= '</select></td>';
	$output .= "<td><input type=submit name='add_actor' value='Add Actor to Movie' /></td>";
	$output .= "</tr>";
	$output .= "</table>";
	
}else{
	$output .= "<h1>Movies</h1>";
	$output .= "<input type='image' title=Edit src='img/icon/add.png' name='add' value='add'/><br>";

	if(isset($_POST['update'])){
		$information  = array();
		$information[0] = $_POST['id'];
		$information[1] = $_POST['title'];
		$information[2] = $_POST['description'];
		$information[3] = $_POST['language'];
		$information[4] = $_POST['regisseur'];

		updateMovie($information);
		$output .= "<font color=green>Movie has been updated</font><br><br>";
		$movies  = getAllMovies();
	}else if(isset($_POST['delete_movie'])){
		$output .= "<font color=green>Movie deleted</font><br><br>";
	}else if(isset($_POST['add_movie'])){
		$add_title = $_POST['title'];
		$add_description = $_POST['description'];
		$add_language = $_POST['language'];
		$add_regisseur = $_POST['regisseur'];
		if(addRegisseur($add_title,$add_description,$add_language,$add_regisseur)){
		$output .= "<font color=green>Movie added</font><br><br>";
		}else{
		$output .= "<font color=red>adding movie failed</font><br><br>";	
		}
		$movies  = getAllMovies();
	}else if(isset($_POST['add_actor'])){
		if(addActorToMovie($_POST['actor'],$_POST['id'])){
		$output .= "<font color=green>Actor added</font><br><br>";
		}else{
		$output .= "<font color=red>adding actor failed</font><br><br>";	
		}
		
	}

	// Chart of all movies
	$output .= "<table width=80%>";
	$output .= "<tr>";
	$output .= "<th>MovieID</th>";
	$output .= "<th>Title</th>";
	$output .= "<th>Description</th>";
	$output .= "<th>Language</th>";
	$output .= "<th>Regisseur</th>";
	$output .= "<th></th>";
	if($_SESSION['user_role'] == 1||$_SESSION['user_role'] == 2){
		$output .= "<th></th>";
	}
	$output .= "</tr>";

	for($i = 0; $i < count($movies); $i++){
		$regisseur_id   = $movies[$i+4];
		$regisseur_name = getRegisseurInformation($movies[$i+4], "name");

		$output .= "<tr>";
		$output .= "<td>".$movies[$i]."</td>";
		$output .= "<td>".$movies[$i+1]."</td>";
		$output .= "<td>".$movies[$i+2]."</td>";
		$output .= "<td>".$movies[$i+3]."</td>";
		$output .= "<td>".$regisseur_name."</td>";
		$output .= "<td><input type='image' title=Show src='img/icon/show_detail.gif' name='show_details' value='$movies[$i]'/></td>";
		if($_SESSION['user_role'] == 1||$_SESSION['user_role'] == 2){
			$output .= "<td><input type='image' title=Edit src='img/icon/edit.gif' name='edit' value='$movies[$i]'/></td>";
		}
		$output .= "</tr>";

		$i = $i + 4;
	}
	$output .= "</table>";
}

//Output
echo $output;
?>